﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace IMark.DAO
{
    public class KhachHangModels
    {
        public static KHACH_HANG DangNhap(string User, string Pass)
        {
            try
            {
                using (EMartEntities db = new EMartEntities())
                {
                    KHACH_HANG kh = db.KHACH_HANG.Where(s => s.TenDangNhap.Equals(User) && s.MatKhau.Equals(Pass)).First();
                    if (kh != null)
                        return kh;
                    else
                        return null;
                };
            }
            catch
            {
                return null;
            }
            
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="kh"></param>
        /// <returns>
        /// 2:Trùng tên đăng nhập;
        /// 3:Trùng Email;
        /// 1: Đăng ký thành công;
        /// 4: Đăng ký thất bại
        /// </returns>
        public static int DangKy(KHACH_HANG kh)
        {
            try
            {
                using (EMartEntities db = new EMartEntities())
                {
                    int result = 1;
                    KHACH_HANG inkh = new KHACH_HANG();
                    if (kh != null)
                    {
                        var queryCheckTenDangNhap = db.KHACH_HANG.Where(s => s.TenDangNhap == kh.TenDangNhap).ToList();
                        if (queryCheckTenDangNhap.Count>0)
                        {
                            result =  2;
                        }
                        else
                        {
                            var queryCheckEmail = db.KHACH_HANG.Where(w => w.Email == kh.Email).ToList();
                            if(queryCheckEmail.Count>0)
                            {
                                result = 3;
                            }
                            else
                            {
                                inkh = kh;
                                db.KHACH_HANG.Add(inkh);
                                db.SaveChanges();
                                result = 1;
                            }
                        }
                    }
                    return result;
                };
            }
            catch
            {
                return 4;
            }
        }
    }
}